package collection.MyList;

import java.util.LinkedList;

public class MySingleList {
    //单链表
    static class ListNode{

        public int val;//节点的值域
        public ListNode next;//下一个节点的地址
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;//当前链表的头节点
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if (head == null){
            head = node;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
        /*
        if (head == null){
            ListNode node = new ListNode(data);
            head = node;
        }else {
            ListNode cur = head;
            while(cur.next != null){
                cur = cur.next;
            }
            ListNode node = new ListNode(data);
            cur.next = node;
        }
         */
    }
    public void addIndex(int index,int data){
        if (index<0||index>size()){
            System.out.println("index不合法");
            return;
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
        }
        ListNode node = new ListNode(data);
        /*
        for (int i = 1; i < index-1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
         */
        ListNode cur = findIndexSubOne(index);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode findIndexSubOne(int index){
        ListNode cur = head;
        while (index-1!=0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //删除首个值为key的节点
    public void remove(int key){
        if (head == null){
            return;
        }
        if (head.val ==key){
            head = head.next;
            return;
        }
        ListNode cur = searchPrey(key);
        if (cur == null){
            System.out.println("没有要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    private ListNode searchPrey(int key){
        ListNode cur = head;
        while (cur.next != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (head == null){
            return;
        }
        ListNode cur = head;
        while(cur.next != null){
            if (cur.val == key){
                ListNode prev = cur;
                ListNode del = cur.next;
                prev.next = del.next;
            }
            cur = cur.next;
        }
        if (head.val ==key){
            head = head.next;
        }
    }
    public void clear(){
        this.head = null;

    }
    public void  fanzhuan(){
        if (head == null || head.next == null){
            return;
        }
        ListNode cur = head;
        /*
        ListNode node = cur.next;
        while (node != null){
            cur.next = head;
            head = cur;
            cur = node;
            node = cur.next;
        }
        cur.next = head;
        head = cur;
        */
         ListNode curNext = null;
         while (cur != null){
             curNext = cur.next;
             cur.next = head;
             head = cur;
             cur = curNext;
         }
    }
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if(head == null || head.next == null || left == right){
            return head;
        }
        ListNode cur = head;
        for(int i = 1;i < left;i++){
            cur = cur.next;
        }
        ListNode curNext = null;
        for(int i = left;i < right;i++){
            curNext = cur.next;
            cur.next = cur;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    public static void main(String[] args) {
        MySingleList singleList = new MySingleList();
        singleList.addLast(2);
        //singleList.createList();
        singleList.display();
        System.out.println(singleList.size());
        System.out.println(singleList.contains(12));
        System.out.println(singleList.contains(13));
        singleList.addFirst(1);
        singleList.addFirst(1);
        singleList.display();
        singleList.addLast(2);
        singleList.addLast(2);
        singleList.display();
        singleList.addIndex(2,3);
        singleList.display();
        singleList.remove(3);
        singleList.display();
        singleList.removeAllKey(1);
        singleList.display();
        singleList.clear();
        singleList.addLast(1);
        singleList.addLast(2);
        singleList.addLast(3);
        singleList.addLast(4);
        singleList.addLast(5);
        singleList.display();
        singleList.fanzhuan();
        singleList.display();
        singleList.clear();
        /*
        singleList.addLast(1);
        singleList.addLast(2);
        singleList.addLast(3);
        singleList.addLast(4);
        singleList.addLast(5);
        singleList.reverseBetween(singleList.head,2,4);
        singleList.display();
         */


    }
}
